package com.jijia.webbase.config.security;

import com.jijia.webbase.domain.User;
import com.jijia.webbase.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

// 3. 自定义UserDetailsService
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
//        遍历user的所有属性 如果为空 则设置 空
        user.setUsername(user.getUsername()==null?"":user.getUsername());
        user.setPassword(user.getPassword()==null?"":user.getPassword());

        return new LoginUser(user);
    }
}